package com.boarsoft.boar.sys.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.sys.SysFuncBiz;
import com.boarsoft.boar.sys.entity.SysFunc;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 系统功能管理
 * 
 * @author Mac_J
 *
 */
@RestController
@RequestMapping("/sys/func")
public class SysFuncAction {
	private static final Logger log = LoggerFactory.getLogger(SysFuncAction.class);

	@Autowired
	private SysFuncBiz sysFuncBiz;
	// @Autowired
	// private ConfigVarSync configVarSync;

	/**
	 * 查询系统功能列表
	 * 
	 * @param key
	 * @param orderBy
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@JsonIgnoreProperties({ "roles" })
	@RequestMapping("/list.do")
	@Authorized(code = "sys.func.list")
	public ReplyInfo<Object> list(String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<SysFunc> pr = sysFuncBiz.list(key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, pr);
	}

	@RequestMapping("/save.do")
	@Authorized(code = "sys.func.save")
	public ReplyInfo<Object> save(SysFunc o) {
		SysFunc a = sysFuncBiz.get(o.getCode());
		if (a == null) {
			sysFuncBiz.add(o);
			return ReplyInfo.SUCCESS;
		}
		a.setMemo(o.getMemo());
		a.setName(o.getName());
		a.setNo(o.getNo());
		a.setType(o.getType());
		a.setUri(o.getUri());
		a.setStatus(o.getStatus());
		sysFuncBiz.update(a);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 删除某个（未使用的）系统功能
	 * 
	 * @param code
	 * @return
	 */
	@RequestMapping("/delete.do")
	@Authorized(code = "sys.func.delete")
	public ReplyInfo<Object> delete(String code) {
		SysFunc o = sysFuncBiz.get(code);
		if (o == null) {
			return Constants.REPLY_INFO_NOTFOUND;
		}
		if (o.getStatus() != SysFunc.STATUS_NOUSE) {
			return Constants.REPLY_INFO_INUSE;
		}
		try {
			// configVarSync.delete(InetUtil.getAddr(), code, o.getSync(),
			// o.getFileId());
			sysFuncBiz.delete(code);
			return ReplyInfo.SUCCESS;
		} catch (IllegalStateException e) {
			String m = e.getMessage();
			if (Util.strIsNotEmpty(m) && m.startsWith("No provider available")) {
				return ReplyInfo.SUCCESS;
			}
			log.error("Error on sync config key {}", code, e);
			return Constants.REPLY_ERROR_INTERNAL;
		}
	}

	/**
	 * 启用某个系统功能
	 * 
	 * @param code
	 * @return
	 */
	@RequestMapping("/start.do")
	@Authorized(code = "sys.func.start")
	public ReplyInfo<Object> start(String code) {
		// SysFunc o = sysFuncBiz.start(code);
		// configVarSync.put("sys-func", code, o.getStatus());
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 禁用某个系统功能
	 * 
	 * @param code
	 * @return
	 */
	@RequestMapping("/stop.do")
	@Authorized(code = "sys.func.stop")
	public ReplyInfo<Object> stop(String code) {
		// SysFunc o = sysFuncBiz.stop(code);
		// configVarSync.put("sys-func", code, o.getStatus());
		return ReplyInfo.SUCCESS;
	}

	// public boolean push(String code) {
	// SysFunc o = sysFuncBiz.get(code);
	// // 根据SysFunc配置的同步器，决定是否需要发送功能开关状态变化的通知
	// String key = "sys.func.status/".concat(code);
	// try {
	// configVarSync.put(InetUtil.getAddr(), key, o.getStatus());
	// } catch (Exception e) {
	// log.error("Error on push config key: {}", code, e);
	// }
	// return false;
	// }
}